<?php

namespace StudyBuddy\Template;

use StudyBuddy\StudyBuddyObject;
use StudyBuddy\Registry;

/**

 * Class for handling creation of tabbed navigation
 * and passing the name of the 'currently active' tab.
 * This will greate the "You are here" effect - the current
 * 'active' tab will be styled as "active" while other like
 * regular tags.
 *
 */
class Urhere extends StudyBuddyObject {

    /**
     * Array of vars that will be passed to template
     * we need to first set one as 'current'
     * and also translate the rest (the ones that do not
     * end with _c)
     *
     * @var array
     */
    protected $vars = array(
        'newest_c' => '',
        'voted_c' => '',
        'active_c' => '',
        'newest' => 'Newest',
        'newest_t' => 'Most recent questions',
        'voted' => 'Most Voted',
        'voted_t' => 'Questions with most votes');

    /**
     * @todo instead of registry pass I18n here
     * we going to use it to translate strings
     * in template
     *
     * @param Registry $oRegistry
     */
    public function __construct(Registry $oRegistry) {
        $this->oRegistry = $oRegistry;
    }

    /**
     *
     * @param string $tpl name of template class file
     *
     * @param string $current name of tab that should
     * be set as current
     *
     * @param array $vars this can be used to pass array of replacement
     * vars to template. Values from this array will be merged
     * with template's own array and if same keys exist then values
     * from this $vars array will override the template's default vars
     *
     * @param Callable function $func if passed will be used
     * by template as callback function. If you need to pass callback
     * but not passing any array $vars then the right way to call
     * this method is $obj->get('tmpSomeTemplate', 'someparam', null, $func)
     */
    public function get($tpl, $current = '', array $vars = null, $func = null) {
        $Tr = $this->oRegistry->Tr;
        $template = $tpl;
        $aVars = $template::getVars();
        if (array_key_exists($current . '_c', $aVars)) {
            $aVars[$current . '_c'] = 'actived';
        }

        if (null !== $vars) {
            $aVars = array_merge($aVars, $vars);
        }

        d('template: ' . $template . ' $aVars: ' . print_r($aVars, 1));
        $this->translate($template, $aVars);
        if (null !== $func) {
            d('$func is not null');
        }

        return $template::parse($aVars, false, $func);
    }

    /**
     * Apply Translation to some template strings
     * 
     * @param string $template
     * @param array $vars passed by reference
     */
    protected function translate($template, array &$vars) {
        $Translator = $this->oRegistry->Tr;
        switch ($template) {
            case 'tplToptabs':
                $vars['questions'] = $Translator['Questions'];
                $vars['unanswered'] = $Translator['Unanswered'];
                $vars['tags'] = $Translator['Tags'];
                $vars['ask'] = $Translator['Ask Question'];
                $vars['users'] = $Translator['Members'];
                break;

            case 'tplQtypesdiv':
                $vars['newest'] = $Translator['Newest'];
                $vars['newest_t'] = $Translator['Most recent questions'];
                $vars['voted'] = $Translator['Most Voted'];
                $vars['voted_t'] = $Translator['Questions with most votes'];
                $vars['active'] = $Translator['Popular'];
                $vars['active_t'] = $Translator['Questions with most popular'];
                break;

            case 'tplQuntypes':
                $vars['newest'] = $Translator['Unanswered'];
                $vars['newest_t'] = $Translator['Questions with no accepted answer'];
                $vars['noanswer'] = $Translator['No Answers'];
                $vars['noanswer_t'] = $Translator['Questions with no answers'];
                break;
        }
    }

}
